# GPL3 or any later version

import os
import json
import time

import gi
import threading
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GLib

from modules import wallet
from modules import totals

def dialog(win):

    # This is the settings window

    dialogWindow = Gtk.Dialog("Settings",
            buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                     Gtk.STOCK_OK, Gtk.ResponseType.OK),
        )
    dialogWindow.set_size_request(400, 400)
    box = dialogWindow.get_content_area()
    
    data = wallet.get_totals(win.wallet)

    # Curranty

    def on_currancy(w):
        data["currancy"] = w.get_text()
        
    ebox = Gtk.HBox()
    ebox.pack_start(Gtk.Label("Currancy:"), 0,0,5)
    box.pack_start(ebox, 0,0,5)
    currancy_entry = Gtk.Entry()
    currancy_entry.connect("changed", on_currancy)
    currancy_entry.set_text(data.get("currancy", "$"))
    ebox.pack_end(currancy_entry, 0,0,5)
    
    # Payday

    def on_payday(w):
        data["payday"] = int(w.get_value())

        
    ebox = Gtk.HBox()
    ebox.pack_start(Gtk.Label("Payday: Every month at"), 0,0,5)
    box.pack_start(ebox, 0,0,5)
    payday_adjust = Gtk.Adjustment(data.get("payday", 10),
                                   lower=1,
                                   upper=28,
                                   step_increment=1) 
    payday_entry  = Gtk.SpinButton(adjustment=payday_adjust,
                                   digits=0)
    payday_entry.connect("changed", on_payday)

    ebox.pack_end(payday_entry, 0, 0, 5) 
    

    box.show_all()
    
    response = dialogWindow.run()
    if response == Gtk.ResponseType.OK:
        wallet.save_totals(win.wallet, data)
        totals.update_totals(win)
        
    dialogWindow.destroy()
